package com.onlinemovies.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.onlinemovies.entity.BookingDetails;
import com.onlinemovies.entity.HallDetails;
import com.onlinemovies.entity.MovieDetails;
import com.onlinemovies.entity.User;
import com.onlinemovies.service.HallService;
import com.onlinemovies.service.MovieService;
import com.onlinemovies.service.UserService;


@Controller
@RequestMapping(value = { "" })
public class HomeController {

	@Autowired
	private UserService userService;	
	
	
	@Autowired	
	private MovieService movieService;
	
	@Autowired
	private HallService hallService;	
	
	@RequestMapping(value = { "", "/" })
	public ModelAndView home() throws Exception {

		Map<String, Object> params = new HashMap<String, Object>();

		List<User> users = userService.getAll();

		params.put("users", users);

		params.put("user", new User());

		return new ModelAndView("index", params);
	}

	@RequestMapping("/save")
	public ModelAndView save(@ModelAttribute("user") User user)
			throws Exception {

		Map<String, Object> params = new HashMap<String, Object>();

		if (!StringUtils.hasText(user.getUsername())) {

			params.put("usernameMessage", "Input required");

			return new ModelAndView("home", params);

		}

		userService.doRegistration(user);
		params.put("usernameMessage", "You have successfully registered.");
		return new ModelAndView("home", params);
	}

	@RequestMapping("/registration")
	public ModelAndView registration() throws Exception {

		Map<String, Object> params = new HashMap<String, Object>();

		/*List<User> users = userService.getAll();

		params.put("users", users);

		params.put("user", new User());*/

		return new ModelAndView("home", params);
	}
	
	
	@RequestMapping("/about")
	public ModelAndView about() throws Exception {
		return new ModelAndView("about", null);
	}

	@RequestMapping(value = { "/selectmovie" })
	public ModelAndView moviecitylang(HttpSession httpSession) throws Exception {

		Map<String, Object> params = new HashMap<String, Object>();

		List<MovieDetails> movies = movieService.getAll();

		params.put("movieList", movies);

		params.put("movie", new MovieDetails());
		
		List<HallDetails> halls = hallService.getAll();

		params.put("hallList", halls);
		params.put("bookingdetails", new BookingDetails());
		User value=(User) httpSession.getAttribute("user");
		if(value!=null)
		params.put("username", value.getUsername());
		//params.put("bookingdetails", new HallDetails());
		SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
		String date = sdf.format(new Date());
		params.put("movienshowdate", date);
		if(value!=null){
		List<BookingDetails> bookingDetails = movieService.getBookingDetails(value);

		params.put("bookingDetails", bookingDetails);
		}
		return new ModelAndView("movieCityLanguage", params);
	}

	@RequestMapping("/login")
	public ModelAndView login(@ModelAttribute("user") User user,HttpSession httpSession) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("bookingdetails", new BookingDetails());
		boolean flag=true;
		if(user.getUsername()==null || user.getUsername().isEmpty())
			flag=false;
		else if(user.getPassword()==null || user.getPassword().isEmpty())
			flag=false;
		if(flag)
		flag=userService.doLogIn(user,httpSession);
		if(flag){
			User value=(User) httpSession.getAttribute("user");
			params.put("username", value.getUsername());
			if(value.getUsertype()==null || value.getUsername().isEmpty()){
				List<MovieDetails> movies = movieService.getAll();

				params.put("movieList", movies);

				params.put("movie", new MovieDetails());
				
				List<HallDetails> halls = hallService.getAll();

				params.put("hallList", halls);
				SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
				String date = sdf.format(new Date());
				params.put("movienshowdate", date);
				
				List<BookingDetails> bookingDetails = movieService.getBookingDetails(value);

				params.put("bookingDetails", bookingDetails);
				return new ModelAndView("movieCityLanguage", params);
			}
			else
				return new ModelAndView("movieCityLanguage", params);
		}else{
			params.put("usernameerr", "Check your username and password");
			return new ModelAndView("index", params);
		}
	}
	
	@RequestMapping("/logout")
	public ModelAndView logout(HttpSession httpSession) throws Exception {
		userService.dologout(httpSession);
		Map<String, Object> params = new HashMap<String, Object>();

		List<User> users = userService.getAll();

		params.put("users", users);

		params.put("user", new User());

		return new ModelAndView("index", params);
	}
}